package com.example.dessert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dessert.dao.ProductMapper;
import com.example.dessert.entity.Product;
import com.example.dessert.service.ProductService;
import com.example.dessert.util.ResultCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: ProductServiceImpl
 * @Description:
 * @Author li
 * @Date 2022/1/11
 * @Version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;


    @Override
    public IPage<Product> queryProductListBuyCategory(Long category_id, Integer current) {

        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",category_id);
        wrapper.eq("disabled",0);
        wrapper.orderByDesc("new_arrival");
        wrapper.orderByDesc("created_at");

        Page<Product> page = new Page<>(current, ResultCode.DEFAULT_PAGE_SIZE);
        IPage<Product> productIPage = productMapper.selectPage(page, wrapper);

        return productIPage;



    }

    @Override
    public Product queryProductBuyId(Long id) {

        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return productMapper.selectOne(wrapper);

    }

    @Override
    public List<Product> queryRecommendProduct(){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("recommend",1);
        wrapper.eq("disabled",0);
        wrapper.orderByDesc("created_at");
        wrapper.last("limit 4");
        return  productMapper.selectList(wrapper);

    }
}
